from django.db import models

from django.contrib.auth import get_user_model  # 导入 django 自带用户
from django.utils.html import format_html

Student = get_user_model()  # 学生对象


# Create your models here.


class SchoolFaculty(models.Model):
    """
    院系表
    """
    name = models.CharField(verbose_name="院系名称", max_length=200, unique=True)

    create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    update_time = models.DateTimeField(verbose_name="编辑时间", auto_now=True)

    class Meta:
        verbose_name = "院系信息"
        verbose_name_plural = "院系信息列表"

    def get_faculty_major(self):
        majors = FacultyMajor.objects.filter(faculty__id=self.id)
        return ",".join([major.name for major in majors])

    # 新字段的显示的名称，相当于 verbose_name
    get_faculty_major.short_description = format_html('<span class="text">院系专业</span>')
    # 指定排序方式，id 倒序排列
    get_faculty_major.admin_order_field = "-id"

    def __str__(self):
        return self.name


class Course(models.Model):
    """
    课程表
    """
    name = models.CharField(verbose_name="课程名称", max_length=200, unique=True)

    create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    update_time = models.DateTimeField(verbose_name="编辑时间", auto_now=True)

    class Meta:
        verbose_name = "课程信息"
        verbose_name_plural = "课程信息列表"

    def __str__(self):
        return self.name


class FacultyMajor(models.Model):
    """
    专业及专业课程表
    """
    name = models.CharField(verbose_name="专业名称", max_length=200, unique=True)
    faculty = models.ForeignKey(verbose_name="所属院系", related_name="faculty_major_set", to=SchoolFaculty,
                                on_delete=models.DO_NOTHING)
    courses = models.ManyToManyField(verbose_name="专业课程", to=Course)

    create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    update_time = models.DateTimeField(verbose_name="编辑时间", auto_now=True)

    class Meta:
        verbose_name = "专业信息与课程"
        verbose_name_plural = "专业信息与课程列表"

    def get_course_to_str(self):
        # 把 专业的课程转成字符串方便显示到 admin
        return ",".join([tag.name for tag in self.courses.get_queryset()])

    # 新字段的显示的名称，相当于 verbose_name
    get_course_to_str.short_description = format_html('<span class="text">专业课程</span>')
    # 指定排序方式，id 倒序排列
    get_course_to_str.admin_order_field = "-id"

    def __str__(self):
        return self.name


class Semester(models.Model):
    """
    学期表
    """
    name = models.CharField(verbose_name="学期名称", max_length=200, unique=True)

    create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    update_time = models.DateTimeField(verbose_name="编辑时间", auto_now=True)

    class Meta:
        verbose_name = "学期信息"
        verbose_name_plural = "学期信息列表"

    def __str__(self):
        return self.name


class StudentCourse(models.Model):
    """
    学生课程成绩表
    """
    student = models.ForeignKey(verbose_name="所属学生", related_name="student_course_set", to=Student,
                                on_delete=models.DO_NOTHING)
    course = models.ForeignKey(verbose_name="所属课程", related_name="course_student_set", to=Course,
                               on_delete=models.DO_NOTHING)
    score = models.SmallIntegerField(verbose_name="课程得分", default=80)
    semester = models.ForeignKey(verbose_name="考试学期", to=Semester, on_delete=models.DO_NOTHING)

    class Meta:
        verbose_name = "学生课程成绩信息"
        verbose_name_plural = "学生课程成绩信息列表"

    def __str__(self):
        return "%s_%s_%s" % (self.student.uid, self.student.username, self.course.name)
